function allReadyEdit() {
    //设置表头控件默认高度
    NG.setFieldsHeight(18);
    if (otype == 'add') {
        //新增初始化
        var v_cgr = NG.execServer('get_hrid_by_fg3userid', {'phid': $appinfo.userID}, 'hrid');
        NG.setValue('empid', v_cgr);//1.2
        var v_ny = NG.dateFormat(NG.getValue('bill_dt'), 'YYYYmm');//1.3
        NG.setValue('ny', v_ny);
        var v_year = NG.dateFormat(NG.getValue('bill_dt'), 'YYYY');//1.4
        NG.setValue('year', v_year);
        var v_month = NG.dateFormat(NG.getValue('bill_dt'), 'mm');//1.5
        NG.setValue('month', v_month)
        NG.setValue('to_lastday', NG.getYmts());//1.6
        NG.setValue('ysrq_wb', NG.dateFormat(NG.getValue('bill_dt'), 'YYYYmmdd'));//1.7
        NG.setValue('cgrq', NG.getValue('bill_dt'));//1.8
        NG.setValue('sdrq', new Date());//1.9
        NG.setValue('empid3', v_cgr);//1.10
        var v_deptid2 = NG.execServer('get_dept_by_logid', {'cno': $appinfo.logid}, 'phid_dept');
        NG.setValue('deptid2', v_deptid2);//1.11
        var v_ytpb = NG.callServer('add_init_ytlb', {
            'opteror': $appinfo.userID,
            'ocode': NG.getValue('ocode')
        }, 'ytlb');
        NG.setValue('ytlb', v_ytpb);//1.12
    }
    //4.1.2
    NG.bindMasterSql('ocode', 'ck', 'get_jdck_by_ocode', {'oc': 'ocode', 'yt': 'ytlb'});
    //4.2.3
    NG.bindMasterSql('ytlb', 'ck', 'get_jdck_by_ocode', {'oc': 'ocode', 'yt': 'ytlb'});
    //4.2.2
    NG.bindMasterSql('ytlb', 'is_jlck', 'sfjljdck', {'oc': 'ocode', 'yt': 'ytlb'});
    //4.2.4  4.2.5  4.2.6
    NG.getItem('ytlb').addListener('helpselected', function (record) {
        switch (record.code) {
            case '10000':
                NG.setValue('ytlb_ywsbm', 'CL');
                NG.setValue('ytlb_cwsbm', 'A');
                NG.setValue('hc_bz', 500);
                break;
            case '10003':
                NG.setValue('ytlb_ywsbm', 'BG');
                NG.setValue('ytlb_cwsbm', 'B');
                NG.setValue('hc_bz', 500);
                break;
            case '10004':
                NG.setValue('ytlb_ywsbm', 'AQ');
                NG.setValue('ytlb_cwsbm', 'C');
                NG.setValue('hc_bz', 1000);
                break;
            case '10005':
                NG.setValue('ytlb_ywsbm', 'ZJ');
                NG.setValue('ytlb_cwsbm', 'D');
                NG.setValue('hc_bz', 1000);
                break;
            default:
                NG.setValue('ytlb_ywsbm', '');
                NG.setValue('ytlb_cwsbm', '');
                NG.setValue('hc_bz', 500);
                break;
        }
    });
    //4.2.7
    NG.bindMasterSql('ytlb', 'gdzc_bz', 'get_bz_by_ocode', {'ocode': 'ocode'});
    //4.2.12
    NG.bindMasterSql('ytlb', 'bm_xh', 'get_xh', {'ytlb': 'ytlb', 'ocode': 'ocode', 'year': 'year'});
    //4.3.1  4.3.2  4.3.3
    NG.getItem('ck').addListener('helpselected', function (record) {
        NG.setValue('to_ck', NG.getValue('ck'));
        var sd = NG.callServer('get_sd', {'phid': NG.getValue('ck')}, 'sd');
        if (sd > 0) {
            //弹出锁定提示
            var sd_msg = NG.callServer('get_sd_msg', {'phid': NG.getValue('ck')});
            NG.alert('温馨提示：' + sd_msg.record[0].title + '已于' + sd_msg.record[0].sdrq + '因' + sd_msg.record[0].sdsj + '被' + sd_msg.record[0].cname + '锁定，请先处理完相关业务解锁后再行录入。');
        }
    });
    //4.4.1
    NG.getItem('ocode_tax').addListener('helpselected', function (record) {
        if (record.data.ocode == '999') {
            if (record.data.ocode == '101') {
                //005
                NG.setValue('jzdw', 255200902000013);
            } else if (record.data.ocode == '102') {
                //010
                NG.setValue('jzdw', 255200902000012);
            } else if (record.data.ocode == '104') {
                //015
                NG.setValue('jzdw', 255200902000060);
            } else if (record.data.ocode == '103') {
                //016
                NG.setValue('jzdw', 255200902000034);
            } else {
                NG.setValue('jzdw', NG.getValue('ocode'));
            }
        } else {
            NG.setValue('jzdw', record.data.phid);
        }
    });
    //4.5.1  4.5.2  4.5.3
    NG.getItem('fplb').addListener('change', function (p1, p2) {
        if (p2 == 'zp') {
            NG.setDetailUnVisible(['pphj']);
            NG.setValue('zzssl', 13);
        } else if (p2 == 'pp') {
            NG.setDetailUnVisible(['prc0', 'amt0', 'se', 'hshj', 'sedj']);
        }
    });
    //4.6.1
    NG.bindMasterSql('sqd', 'cgtj', 'get_cgtj_by_sqd', {'phid': 'sqd'});
    //4.6.2   4.6.3    4.6.4    4.6.5
    NG.getItem('sqd').addListener('helpselected', function (record) {
        var hasvalue = NG.validateEmpty(['zzssl', 'ocode', 'ytlb', 'hc_bz', 'fplb']);
        if (hasvalue) {
            var pars1 = {
                'zzssl': NG.getValue('zzssl'),
                'ocode': NG.getValue('ocode'),
                'ytlb': NG.getValue('ytlb'),
                'hc_bz': NG.getValue('hc_bz'),
                'fplb': NG.getValue('fplb'),
                'phid': record.data.phid
            };
            var pars2 = {
                'ocode': NG.getValue('ocode'),
                'ytlb': NG.getValue('ytlb'),
                'hc_bz': NG.getValue('hc_bz'),
                'fplb': NG.getValue('fplb'),
                'phid': record.data.phid
            };
            NG.addDetailBySql('get_item_by_sqd1', pars1, false);
            NG.addDetailBySql('get_item_by_sqd2', pars2, false);
            NG.addDetailBySql('get_item_by_sqd3', pars1, false);
            NG.addDetailBySql('get_item_by_sqd4', pars2, false);
        } else {
            NG.getItem('sqd').clearValue();
        }
    });
    //4.7.1
    NG.bindMasterSql('vendor_id', 'gffzr', 'get_gysfzr', {'phid': 'vendor_id'});
    //4.8.1
    NG.bindMasterSql('empid', 'deptid', 'get_dept_by_empid', {'phid': 'empid'});
    NG.getItem('empid').fireEvent('helpselected');//此处特殊处理，为了新增时带出采购人对应的采购部门
    //4.9.1  4.9.2  4.9.3  4.9.4
    NG.getItem('bill_dt').addListener('change', function (p1, p2) {
        NG.setValue('ny', NG.dateFormat(NG.getValue('bill_dt'), 'YYYYmm'));
        NG.setValue('year', NG.dateFormat(NG.getValue('bill_dt'), 'YYYY'));
        NG.setValue('month', NG.dateFormat(NG.getValue('bill_dt'), 'mm'));
        NG.setValue('ysrq_wb', NG.dateFormat(NG.getValue('bill_dt'), 'YYYYmmdd'));
    });
    //4.10.1
    NG.bindMasterSql('year', 'bm_xh', 'get_bmsxh', {'ytlb': 'ytlb', 'ocode': 'ocode', 'year': 'year'});
    NG.getItem('year').fireEvent('change');//同4.8.1一样此处需要特殊处理，年份带出的时候，4.10.1还未执行，导致新增时bm_xh为空
    //4.11.1
    NG.bindMasterSql('to_ck', 'zxm_pc', 'get_pc_by_ck', {'phid': 'to_ck'});
    //4.11.2    xz_bm_to_pc视图不存在
    NG.bindMasterSql('to_ck', 'cfcc_xz', 'get_cklx_by_ck', {'phid': 'to_ck'});
    //4.11.3
    //4.12.1
    NG.bindMasterSql('empid1', 'bm', 'get_dept_by_empid', {'phid': 'empid1'});
    //4.13.1
    NG.getItem('bm').addListener('change', function (p1, p2) {
        NG.setValue('deptid1', p2);
    });
    //4.16.1
    NG.getItem('ocode').addListener('helpselected', function () {
        form843_setTitle();
    });
    //4.17.1
    NG.getItem('ytlb').addListener('helpselected', function () {
        form843_setTitle();
    });
    //4.15.1
    NG._getGrid().getColumn('zyxz_name').getEditor().on('helpselected', function () {
        form843_setTitle();
    });
    //4.18.1  4.18.2  4.18.3
    NG.getItem('zxm_pc').on('change', function (p1, p2) {
        var wbs = NG.callServer('get_wbs_by_pc', {'pcid': p2}, 'rel_wbscode');
        var cbs = NG.callServer('get_cbs_by_pc', {'pcid': p2}, 'cbs_code');
        var nc = NG.callServer('get_nc_name_by_pc', {'pcid': p2}, 'nc_name');
        NG.setValue('wbs_code', wbs);
        NG.setValue('cbs_code', cbs);
        NG.setValue('nc_xmmc', nc);
    });
    //4.19.1
    NG.getItem('bm_xh').on('change', function (p1, p2) {
        var v_bill_no = NG.getValue('ytlb_ywsbm') + 'RK-' + NG.getValue('ocode') + '-' + NG.getValue('year') + '-' + NG.getValue('bm_xh');
        NG.setValue('bill_no', v_bill_no);
    });
    //5.1.1~5.1.7
    NG._getGrid().getColumn('zyxz_name').getEditor().on('helpselected', function (record) {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if (curRow) {
            curRow.data.rownum = 1;
            curRow.data.itemno = record.data.itemno;
            curRow.data.gg = record.data.spec;
            curRow.data.dw = record.data.phid_msunit;
            curRow.data.dw_name = record.data.msname;
            var ypmc = NG.callServer('get_detail_ypmc', {'cpxh': curRow.data.cpxh, 'phid': record.data.phid}, 'ypmc');
            curRow.data.yp_name = ypmc;
            var mainmat = NG.callServer('get_mainmat', {'phid': record.data.phid}, 'main_mat') || '';
            curRow.data.mrzclx = mainmat;
            curRow.data.ytlb = NG.getValue('ytlb');
            NG._getGrid().getView().refresh();
        }
    });
    //5.2.1
    NG._getGrid().getColumn('cpxh').getEditor().on('blur', function (p1) {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if (curRow && curRow.data.zyxz) {
            var ypmc = NG.callServer('get_detail_ypmc', {'cpxh': p1.getValue(), 'phid': curRow.data.zyxz}, 'ypmc');
            curRow.data.yp_name = ypmc;
            NG._getGrid().getView().refresh();
        }
    });
    //5.3.1  5.3.2  5.3.3
    NG._getGrid().getColumn('qty').getEditor().on('change', function (p1, p2) {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if (curRow) {
            curRow.data.prc0 = (curRow.data.amt0 || 0) / p2;
            curRow.data.sedj = (curRow.data.se || 0) / p2;
            curRow.data.hsdj = (curRow.data.hshj || 0) / p2;
            NG._getGrid().getView().refresh();
        }
    });
    //5.4.1
    NG._getGrid().getColumn('pphj').getEditor().on('change', function (p1, p2) {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if (curRow) {
            curRow.data.amt0=p2;
            NG._getGrid().getView().refresh();
        }
    });
    //5.5.1  5.5.2  5.5.4
    NG._getGrid().getColumn('amt0').getEditor().on('change', function () {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if (curRow&&curRow.data.qty) {
            curRow.data.hshj=(curRow.data.amt0 || 0)+(curRow.data.se || 0);
            curRow.data.prc0 = (curRow.data.amt0 || 0) / curRow.data.qty;
            curRow.data.se = (curRow.data.amt0||0)*(NG.getValue('zzssl')||0)/100;
            NG._getGrid().getView().refresh();
        }
    });
    //5.5.3
    NG.setSumToMaster('amt0','amt0');
    //5.6.1  5.6.2
    NG._getGrid().getColumn('se').getEditor().on('change', function () {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if (curRow&&curRow.data.qty) {
            curRow.data.hshj=(curRow.data.amt0 || 0)+(curRow.data.se || 0);
            curRow.data.sedj = (curRow.data.se || 0) / curRow.data.qty;
            NG._getGrid().getView().refresh();
        }
    });
    //5.6.3
    NG.setSumToMaster('sehj','se');

    //5.7.1~5.7.11
    NG._getGrid().getColumn('zclx_name').getEditor().on('helpselected', function (record) {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if (curRow) {
            //5.7.1
            if(record.data.c_no=='1'){
                if(NG.getValue('ytlb')=='10004'){
                    curRow.data.jczl=0;
                }
                else {
                    curRow.data.jczl=0.05;
                }
            }
            //5.7.2
            if(record.data.c_no=='1'){
                if(NG.getValue('ytlb')=='10004'){
                    curRow.data.zjhtxfs='12';
                }
                else {
                    curRow.data.zjhtxfs='11';
                }
            }
            else if(record.data.c_no=='2'||record.data.c_no=='4'){
                curRow.data.zjhtxfs='22';
            }
            //5.7.3
            if(record.data.c_no=='1'){
                if(curRow.data.gdzclb=='02'){
                    curRow.data.syyx=120;
                }
                else if(curRow.data.gdzclb=='03'){
                    curRow.data.syyx=36;
                }
                else if(curRow.data.gdzclb=='04'){
                    curRow.data.syyx=48;
                }
                else if(curRow.data.gdzclb=='05'){
                    curRow.data.syyx=60;
                }
                else if(curRow.data.gdzclb=='06'){
                    curRow.data.syyx=0;
                }
                else {
                    curRow.data.syyx=36;
                }
            }
            //5.7.5    5.7.6   5.7.7
            if(record.data.c_no=='1'){
                curRow.data.gdzc_rownum=curRow.data.gdzc_rownum+1;
                curRow.data.cwbmtz=NG.getValue('gdzc_rownum')||'';
                var gdzclb = NG.callServer('get_gdzclb',{'phid':curRow.data.zyxz},'user_zclxpb');
                if(gdzclb){
                    curRow.data.gdzclb = gdzclb;
                }
            }
            NG.setValue('gdzc_sl',curRow.data.rownum);//5.7.8
            NG.setValue('dzyhp_sl',curRow.data.rownum);//5.7.9
            NG.setValue('hc_sl',curRow.data.rownum);//5.7.10
            NG.setValue('zzcl_sl',curRow.data.rownum);//5.7.11
            NG._getGrid().getView().refresh();
        }
    });

    //5.8.1  5.8.2
    NG._getGrid().getColumn('gdzclb_name').getEditor().on('helpselected', function (record){
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if(curRow){
            if(curRow.data.zclx=='1'){
                curRow.data.syyx=NG.callServer('get_syyx_by_gdzclx',{'phid':curRow.data.gdzclb||0},'syyx')||0;
                //fzyp_max_cwbm 视图没有
            }
            NG._getGrid().getView().refresh();
        }
    });
    //5.9.1     fzyp_max_ypno_dj视图没有
    //5.10.1    fzyp_max_cwbm视图没有
    //5.11.1
    NG._getGrid().getColumn('syyx').getEditor().on('change',function (p1,p2) {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if(curRow&&p2>0){
            var csdj=curRow.data.prc0||0;
            var jczl=curRow.data.jczl||0;
            curRow.data.yzje =Math.round((csdj*(1-jczl)/p2)*100)/100;
            NG._getGrid().getView().refresh();
        }
    });
    //5.12.1    fzyp_max_ypno_dj视图没有
    //5.12.2
    NG.setSpliceToMaster('qd','zyxz_name',',');
    //5.13.1
    NG._getGrid().getColumn('hshj').getEditor().on('change',function (p1,p2) {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if(curRow&&curRow.data.qty){
            curRow.data.hsdj= (curRow.data.hshj||0)/curRow.data.qty;
            NG._getGrid().getView().refresh();
        }
    });
    //5.13.2
    NG.setSumToMaster('hshj','hshj');
    //5.14.1~5.14.3
    NG._getGrid().getColumn('prc0').getEditor().on('change',function (p1,p2) {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if(curRow){
            if(curRow.data.mrzclx=='周转类单件管理_含耗材'){
                if(curRow.data.prc0>=NG.getValue('gdzc_bz')){
                    curRow.data.zclx='1';
                }
                else if(curRow.data.prc0>=NG.getValue('hc_bz')){
                    curRow.data.zclx='2';
                }
                else {
                    curRow.data.zclx='3';
                }
            }
            else if(curRow.data.mrzclx=='周转类单件管理_不含耗材'){
                if(curRow.data.prc0>=NG.getValue('gdzc_bz')){
                    curRow.data.zclx='1';
                }
                else {
                    curRow.data.zclx='2';
                }
            }
            else if(curRow.data.mrzclx=='周转类批量管理'){
                curRow.data.zclx='4';
            }
            else if(curRow.data.mrzclx=='耗材'){
                curRow.data.zclx='3';
            }
            else if(curRow.data.mrzclx=='固定资产'){
                curRow.data.zclx='1';
            }
            else if(curRow.data.mrzclx=='低值易耗品'){
                curRow.data.zclx='2';
            }
            var csdj=curRow.data.prc0||0;
            var jczl=curRow.data.jczl||0;
            if(curRow.data.syyx&&curRow.data.syyx>0){
                curRow.data.yzje=Math.round((csdj*(1-jczl)/curRow.data.syyx )*100)/100;
            }
            curRow.data.cz = csdj*jczl;
            NG._getGrid().getView().refresh();
        }
    });
    //5.16.1  5.16.2
    NG._getGrid().getColumn('jczl').getEditor().on('change',function (p1,p2) {
        var curRow = NG._getGrid().getSelectionModel().getSelection()[0];
        if(curRow){
            var csdj=curRow.data.prc0||0;
            var jczl=curRow.data.jczl||0;
            if(curRow.data.syyx&&curRow.data.syyx>0){
                curRow.data.yzje=Math.round((csdj*(1-jczl)/curRow.data.syyx )*100)/100;
            }
            curRow.data.cz = csdj*jczl;
            NG._getGrid().getView().refresh();
        }
    });
    //5.17.1
    NG.setSumToMaster('rownum','rownum');
    //5.18.1
    NG.setSumToMaster('gdzc_rownum','gdzc_rownum');
    //6.1
    NG.getItem('ytlb').on('helpselected',function () {form843_CheckWbs()});
    NG.getItem('to_ck').on('helpselected',function () {form843_CheckWbs()});
    NG.getItem('wbs_code').on('helpselected',function () {form843_CheckWbs()});
    NG.getItem('cbs_code').on('helpselected',function () {form843_CheckWbs()});
    //6.2
    NG.getItem('to_lastday').on('change',function () {
        if(NG.getValue('to_lastday')<3){
            Ext.MessageBox.alert('提示','温馨提示：当月验收必须当月报财务入账。为保证当月验收的物资能在当月核准入账，每月最后3天停止做该业务！',function () {
                window.close();
            });
        }
    });
    //6.3
    NG.getItem('is_jlck').on('change',function () {
        if(NG.getValue('is_jlck')==0){
            Ext.MessageBox.alert('提示','尚未建立基地仓库',function () {
                window.close();
            });
        }
        else if(NG.getValue('is_jlck')>1){
            Ext.MessageBox.alert('提示','您建立的多个基地仓库，这是不允许的！',function () {
                window.close();
            });
        }
    });

}

//6.1
function form843_CheckWbs() {
    //检查wbs和cbs
    if(NG.getValue('ytlb')=='10004'&&NG.getValue('gdzc_rownum')>0&&NG.getValue('to_ck')&&(NG.getValue('wbs_code')||NG.getValue('cbs_code'))){
        Ext.MessageBox.confirm('提示','拟调往的项目未正确设置WBS或CBS，请联系成本核算科处理后再做本业务！,点击确定将关闭当前单据',function (btn) {
            if(btn=='yes'){
                window.close();
            }
        });
    }
}

function form843_setTitle() {
    //更新标题
    var v_ocode = NG.getItem('ocode').rawValue || '';
    var v_ytlb = NG.getItem('ytlb').rawValue || '';
    var v_zyxz = '';
    if (NG._getGrid().getStore().getCount() > 0) {
        v_zyxz = NG._getGrid().getStore().getAt(0).data.zyxz_name;
    }
    NG.setValue('title', v_ocode + v_ytlb + v_zyxz + '等验收入库');
}

function beforeDeleteList() {
    var arg = {'phid': NG._getGrid().getSelectionModel().getSelection()[0].data.phid};
    //2.1
    var b1 = NG.deleteEvent('delete_event1', arg, '该单据已经写入过仓库，不得删除！若有错误请联系信息中心。');
    //2.2
    var b2 = NG.deleteEvent('delete_event2', arg, '该单据已核准过！！不得删除！！！');
    if (b1 && b2) {
        return true;
    } else {
        return false;
    }
}

function getSaveDataEdit(type) {
    //审核更新事件
    //3.1
    if (type == 'Verify') {
        return {funcname: 'fzyp_rkd_hz', paramstr: [{phid: NG._getGrid().getSelectionModel().getSelection()[0].data.phid}]};
    }
}

function getSaveDataList(type) {
    //审核更新事件
    //3.1
    if (type == 'Verify') {
        return {funcname: 'fzyp_rkd_hz', paramstr: [{phid: NG._getGrid().getSelectionModel().getSelection()[0].data.phid}]};
    }
}
